package com.ruoyi.controller.production;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.production.MesProductionPlan;
import com.ruoyi.service.production.IMesProductionPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 生产计划Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/production/plan")
public class MesProductionPlanController extends BaseController {
    @Autowired
    private IMesProductionPlanService IMesProductionPlanService;

    /**
     * 查询生产计划列表
     */
    @PreAuthorize("@ss.hasPermi('production:plan:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesProductionPlan mesProductionPlan) {
        startPage();
        List<MesProductionPlan> list = IMesProductionPlanService.selectMesProductionPlanList(mesProductionPlan);
        return getDataTable(list);
    }

    /**
     * 导出生产计划列表
     */
    @PreAuthorize("@ss.hasPermi('production:plan:export')")
    @Log(title = "生产计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesProductionPlan mesProductionPlan) {
        List<MesProductionPlan> list = IMesProductionPlanService.selectMesProductionPlanList(mesProductionPlan);
        ExcelUtil<MesProductionPlan> util = new ExcelUtil<MesProductionPlan>(MesProductionPlan.class);
        util.exportExcel(response, list, "生产计划数据");
    }

    /**
     * 获取生产计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('production:plan:query')")
    @GetMapping(value = "/{planId}")
    public AjaxResult getInfo(@PathVariable("planId") Long planId) {
        return success(IMesProductionPlanService.selectMesProductionPlanByPlanId(planId));
    }

    /**
     * 新增生产计划
     */
    @PreAuthorize("@ss.hasPermi('production:plan:add')")
    @Log(title = "生产计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesProductionPlan mesProductionPlan) {
        return toAjax(IMesProductionPlanService.insertMesProductionPlan(mesProductionPlan));
    }

    /**
     * 修改生产计划
     */
    @PreAuthorize("@ss.hasPermi('production:plan:edit')")
    @Log(title = "生产计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesProductionPlan mesProductionPlan) {
        return toAjax(IMesProductionPlanService.updateMesProductionPlan(mesProductionPlan));
    }

    /**
     * 删除生产计划
     */
    @PreAuthorize("@ss.hasPermi('production:plan:remove')")
    @Log(title = "生产计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{planIds}")
    public AjaxResult remove(@PathVariable Long[] planIds) {
        return toAjax(IMesProductionPlanService.deleteMesProductionPlanByPlanIds(planIds));
    }

    /**
     * 校验计划编号是否唯一
     */
    @GetMapping("/checkPlanCodeUnique")
    public AjaxResult checkPlanCodeUnique(String planCode, Long planId) {
        boolean isUnique = IMesProductionPlanService.checkPlanCodeUnique(planCode, planId);
        return AjaxResult.success(isUnique);
    }
}
